package timing.ukulele.seal.minio;

import io.minio.MinioClient;
import okhttp3.HttpUrl;

import java.net.URL;

/**
 * minip客户端封装
 *
 * <p> 参数               类型	        描述</p>
 * <p> endpoint     	string	        endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:</p>
 * <p>                                  https://s3.amazonaws.com</p>
 * <p>                                  https://play.min.io </p>
 * <p>                                  localhost</p>
 * <p>                                  play.min.io</p>
 * <p> port	            int	            TCP/IP端口号。可选，默认值是，如果是http,则默认80端口，如果是https,则默认是443端口。</p>
 * <p> accessKey	    string	        accessKey类似于用户ID，用于唯一标识你的账户。</p>
 * <p> secretKey	    string	        secretKey是你账户的密码。</p>
 * <p> secure	        boolean	        如果是true，则用的是https而不是http,默认值是true。</p>
 * <p> url	            URL	            Endpoint URL对象。</p>
 * <p> url	            HttpURL	        Endpoint HttpUrl对象。</p>
 * <p>
 * 示例
 * // 1. public MinioClient(String endpoint)
 * MinioClient minioClient = new MinioClient("https://play.min.io");
 * <p>
 * // 2. public MinioClient(URL url)
 * MinioClient minioClient = new MinioClient(new URL("https://play.min.io"));
 * <p>
 * // 3. public MinioClient(com.squareup.okhttp.HttpUrl url)
 * MinioClient minioClient = new MinioClient(new HttpUrl.parse("https://play.min.io"));
 * <p>
 * // 4. public MinioClient(String endpoint, String accessKey, String secretKey)
 * MinioClient minioClient = new MinioClient("https://play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
 * <p>
 * // 5. public MinioClient(String endpoint, int port,  String accessKey, String secretKey)
 * MinioClient minioClient = new MinioClient("https://play.min.io", 9000, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
 * <p>
 * // 6. public MinioClient(String endpoint, String accessKey, String secretKey, boolean insecure)
 * MinioClient minioClient = new MinioClient("https://play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", true);
 * <p>
 * // 7. public MinioClient(String endpoint, int port,  String accessKey, String secretKey, boolean insecure)
 * MinioClient minioClient = new MinioClient("https://play.min.io", 9000, "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", true);
 * <p>
 * // 8. public MinioClient(com.squareup.okhttp.HttpUrl url, String accessKey, String secretKey)
 * MinioClient minioClient = new MinioClient(new URL("https://play.min.io"), "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
 * <p>
 * // 9. public MinioClient(URL url, String accessKey, String secretKey)
 * MinioClient minioClient = new MinioClient(HttpUrl.parse("https://play.min.io"), "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
 * </p>
 */
public enum StorageClientFactory {
    INSTANCE;

    public MinioClient createStorageClient(String endpoint, Integer port, String accessKey, String secretKey, Boolean secure, URL url, HttpUrl httpUrl) throws Exception {
        //无账户和密码
        if (accessKey == null || accessKey.length() == 0 || secretKey == null || secretKey.length() == 0) {
            if (endpoint != null && endpoint.length() > 0) {
                return getStorageClient(endpoint);
            }
            if (url != null) {
                return getStorageClient(url);
            }
            if (httpUrl != null) {
                return getStorageClient(httpUrl);
            }
        } else {//有账户和密码
            //配置端口
            if (port != null && port >= 0 && port < 65536) {
                if (secure != null) {
                    return getStorageClient(endpoint, port, accessKey, secretKey, secure);
                }
                return getStorageClient(endpoint, port, accessKey, secretKey);
            } else {//未配置端口
                if (url != null) {
                    return getStorageClient(url, accessKey, secretKey);
                }
                if (httpUrl != null)
                    return getStorageClient(httpUrl, accessKey, secretKey);
                if (secure != null)
                    return getStorageClient(endpoint, accessKey, secretKey, secure);
                return getStorageClient(endpoint, accessKey, secretKey);
            }
        }
        throw new Exception("");
    }

    /**
     * 获取一个客户端
     *
     * @param endpoint endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:
     *                 https://s3.amazonaws.com
     *                 https://play.min.io
     *                 localhost
     *                 play.min.io
     * @return MinioClient
     */
    MinioClient getStorageClient(String endpoint) {
        return MinioClient.builder().endpoint(endpoint).build();
    }

    /**
     * 获取一个客户端
     *
     * @param url Endpoint URL对象
     * @return MinioClient
     */
    MinioClient getStorageClient(URL url) {
        return MinioClient.builder().endpoint(url).build();
    }

    /**
     * 获取一个客户端
     *
     * @param url 对象
     * @return MinioClient
     */
    MinioClient getStorageClient(HttpUrl url) {
        return MinioClient.builder().endpoint(url).build();
    }

    /**
     * 获取一个客户端
     *
     * @param endpoint  endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:
     *                  https://s3.amazonaws.com
     *                  https://play.min.io
     *                  localhost
     *                  play.min.io
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @return MinioClient
     */
    MinioClient getStorageClient(String endpoint, String accessKey, String secretKey) {
        return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
    }

    /**
     * 获取一个客户端
     *
     * @param endpoint  endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:
     *                  https://s3.amazonaws.com
     *                  https://play.min.io
     *                  localhost
     *                  play.min.io
     * @param port      TCP/IP端口号。可选，默认值是，如果是http,则默认80端口，如果是https,则默认是443端口。
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @return MinioClient
     */
    MinioClient getStorageClient(String endpoint, int port, String accessKey, String secretKey) {
        return MinioClient.builder().endpoint(endpoint, port, endpoint.startsWith("https")).credentials(accessKey, secretKey).build();
    }

    /**
     * 获取一个客户端
     *
     * @param endpoint  endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:
     *                  https://s3.amazonaws.com
     *                  https://play.min.io
     *                  localhost
     *                  play.min.io
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @param insecure  如果是true，则用的是https而不是http,默认值是true。
     * @return MinioClient
     */
    MinioClient getStorageClient(String endpoint, String accessKey, String secretKey, boolean insecure) {
        return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
    }

    /**
     * 获取一个客户端
     *
     * @param endpoint  endPoint是一个URL，域名，IPv4或者IPv6地址。以下是合法的endpoints:
     *                  https://s3.amazonaws.com
     *                  https://play.min.io
     *                  localhost
     *                  play.min.io
     * @param port      TCP/IP端口号。可选，默认值是，如果是http,则默认80端口，如果是https,则默认是443端口。
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @param insecure  如果是true，则用的是https而不是http,默认值是true。
     * @return MinioClient
     */
    MinioClient getStorageClient(String endpoint, int port, String accessKey, String secretKey, boolean insecure) {
        return MinioClient.builder().endpoint(endpoint, port, insecure).credentials(accessKey, secretKey).build();
    }

    /**
     * 获取一个客户端
     *
     * @param url       HttpUrl对象
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @return MinioClient
     */
    MinioClient getStorageClient(HttpUrl url, String accessKey, String secretKey) {
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
    }

    /**
     * 获取一个客户端
     *
     * @param url       URL对象
     * @param accessKey accessKey类似于用户ID，用于唯一标识你的账户。
     * @param secretKey secretKey是你账户的密码。
     * @return MinioClient
     */
    MinioClient getStorageClient(URL url, String accessKey, String secretKey) {
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
    }
}
